package com.caishi.lkx.exam.question;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.caishi.lkx.exam.model.QuestionModel;
import com.zzw.common.utils.MD5Util;


import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * 客观题
 */
public abstract class ObjectiveQuestionDataProcess<AS extends Object> extends AbstractQuestionDataProcess implements Question<AS> {


    @Override
    public String questionContentSign(QuestionModel questionModel) {
        String stem = JSON.toJSONString(questionModel.getStem());
        String options = JSON.toJSONString(questionModel.getQuOption());
        String answer = questionModel.getRightAnswerStr();
        String answerDesc = JSON.toJSONString(questionModel.getAnswerDesc());
        return MD5Util.getMessageDigest((stem + options + answer + answerDesc).getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public boolean questionContentCheck(QuestionModel questionModel) {
        return super.questionContentCheck(questionModel) &&
                CollUtil.isNotEmpty(questionModel.getQuOption()) &&
                questionModel.getQuOption().stream().allMatch(v -> ObjectUtil.isNotEmpty(v.get("option")) &&
                        questionDataIsNotEmpty((List<Map<String, Object>>) v.get("content")) &&
                        questionShowDataCheck((List<Map<String, Object>>) v.get("content"))
                ) &&
                ObjectUtil.isNotEmpty(questionModel.getAnswer())
                ;
    }
}
